package com.ruoyi.web.controller.practice;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.PracticePost;
import com.ruoyi.system.service.IPracticePostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/practice/post")
public class PostController extends BaseController
{
    private String prefix = "practice/post";

    @Autowired
    private IPracticePostService practicePostService;

    @RequiresPermissions("practice:post:view")
    @GetMapping()
    public String post()
    {
        return prefix + "/post";
    }

    @RequiresPermissions("practice:post:list")
    @PostMapping("/list")
    @ResponseBody
    public List<PracticePost> list(PracticePost post)
    {
        List<PracticePost> Postlist = practicePostService.selectPostList(post);
        return Postlist;
    }

    @RequiresPermissions("practice:post:add")
    @GetMapping("/add")
    public String add(ModelMap mmap){
        mmap.addAttribute("name", getSysUser().getUserName());
        return prefix + "/add";
    }

    @RequiresPermissions("practice:post:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated PracticePost post)
    {
        post.setDeptId(getSysUser().getDeptId());
        post.setUserId(getSysUser().getUserId());
        return toAjax(practicePostService.insertPost(post));
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("practice:post:edit")
    public String edit(@PathVariable("id") Long id,ModelMap mmap){
        mmap.addAttribute("id", id);
        mmap.addAttribute("name", getSysUser().getUserName());
        return prefix + "/edit";
    }

    @RequiresPermissions("practice:post:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated PracticePost post)
    {
        post.setId(post.getId()*-1);
        return toAjax(practicePostService.updatePost(post));
    }

    @RequiresPermissions("practice:post:remove")
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") int id)
    {
        if(id<0)
            return toAjax(practicePostService.deletePost(id*-1));
        else
            return toAjax(0);
    }
}
